﻿namespace UnityEngine
{
    using System;

    public class AndroidJavaClass : AndroidJavaObject
    {
        public AndroidJavaClass(IntPtr jclass)
        {
            if (jclass == IntPtr.Zero)
            {
                Debug.LogWarning("JNI: Init'd AndroidJavaClass with null ptr!");
            }
            else
            {
                base.m_jclass = AndroidJNI.NewGlobalRef(jclass);
                base.m_jobject = IntPtr.Zero;
            }
        }

        public AndroidJavaClass(string className)
        {
            this._AndroidJavaClass(className);
        }

        private void _AndroidJavaClass(string className)
        {
            base.DebugPrint("Creating AndroidJavaClass from " + className);
            IntPtr clazz = AndroidJNI.FindClass("java/lang/Class");
            IntPtr methodID = AndroidJNI.GetStaticMethodID(clazz, "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
            object[] args = new object[] { className };
            jvalue[] jvalueArray = AndroidJNIHelper.CreateJNIArgArray(args);
            clazz = AndroidJNI.CallStaticObjectMethod(clazz, methodID, jvalueArray);
            base.m_jclass = AndroidJNI.NewGlobalRef(clazz);
            base.m_jobject = IntPtr.Zero;
        }
    }
}

